package com.gn.controller;

import com.gn.compile.PL0;
import com.gn.domain.Code;
import com.gn.domain.PL0Code;
import com.gn.domain.Phrase;
import com.gn.domain.Result;
import com.gn.service.PL0Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/phrase")
public class PhraseController {
    @Autowired
    private PL0Service pl0Service;

    @PostMapping
    public Result insert(@RequestBody PL0Code pl0_code) {
        String code = pl0_code.getCode();
        List<Phrase> phraseList = PL0.getSource(code).executeCompile();

        // 写入数据库
        boolean flag = pl0Service.save(pl0_code);
        return new Result(phraseList, flag ? Code.SAVE_OK : Code.SAVE_ERR, "");
    }
}
